<%

var monitoringDataContainer;
try
{
    monitoringDataContainer = Server.CreateObject("OxWebCATI.Monitoring.DataContainer");
}
catch(e) { }

var MONITORING_DELAY = 1000;

function PrintFormContents()
{
	for(var i = 1; i<= Request.Form.Count; i++)
	{
		Response.Write(Request.Form.Key(i) + ": " + Request.Form.Item(i) + "<br>");
	}
}

function PreparePage()
{
    if(IsBeingMonitored(getRemoteUserName())) monitoringDataContainer.Get(getRemoteUserName()).PreparePage();
}

function Out(data)
{
    Response.Write(data);
    if(IsBeingMonitored(getRemoteUserName())) monitoringDataContainer.Get(getRemoteUserName()).AppendHtml(data);
}

function CompletePage()
{
    if(!IsBeingMonitored(getRemoteUserName())) return;
    Response.Write("<scr" + "ipt>if(document.forms[0]) { document.forms[0].onsubmit = function() { ServerRequest.Execute('/webcati/admin/monitoringDataCollector.asp', _Constants.Method.Post, Array('EVENT_DATA', EventCollector.Serialize()), false); EventCollector.Clear(); return true; } }</scr" + "ipt>");
    monitoringDataContainer.Get(getRemoteUserName()).CompletePage();
}

function AppendEvents(data)
{
    if(IsBeingMonitored(getRemoteUserName())) monitoringDataContainer.Get(getRemoteUserName()).AppendEvents(data);
}

/* COMMON FUNCTIONS */

function StartMonitoring(usr)
{
    monitoringDataContainer.StartMonitoring(usr);
}

function StopMonitoring(usr)
{
    monitoringDataContainer.StopMonitoring(usr);
}

function IsBeingMonitored(usr)
{
    return monitoringDataContainer != null ? monitoringDataContainer.IsBeingMonitored(usr) : false;
}

/* QUERY FUNCTIONS */

function LatestHtmlIndex(usr)
{
    return monitoringDataContainer.Get(usr).LatestHtmlIndex;
}

function IsHtml(usr, idx)
{
    return monitoringDataContainer.Get(usr).IsHtml(idx);
}

function GetData(usr, idx)
{
    return monitoringDataContainer.Get(usr).GetData(idx);
}

function IsDataAvailable(usr, idx)
{
    return monitoringDataContainer.Get(usr).IsDataAvailable(idx);
}
function ReplayEvents(usr, idx)
{
    Response.Write("EventCollector.MonitoringIndex = " + idx + ";");
    if(!IsDataAvailable(usr, idx)) return;
    if(IsHtml(usr, idx)) return;
    
    var speed = LatestHtmlIndex(usr) > parseInt(Session("LAST_PAGE_MONITORED")) ? 1 : 1;
    
    var eventData = GetData(usr, idx);
    if(eventData && eventData != "" && eventData != "undefined")
    {
        // Response.Write("alert('" + eventData + "');");
        Response.Write("EventCollector.Replay(\"" + eventData + "\", " + speed + ");");
    }
}

function RenderObservableScript()
{
    if(!IsBeingMonitored(getRemoteUserName())) return;
%>
    <script language="javascript">
        Mouse.Initialize();
        EventCollector.Initialize();
        window.setInterval("EventCollector.RegisterEvent(EventCollector.MOUSE_POSITION, null, Mouse.X + ',' + Mouse.Y);", 25);
        window.setInterval("ServerRequest.Execute('/webcati/admin/monitoringDataCollector.asp', _Constants.Method.Post, Array('EVENT_DATA', EventCollector.Serialize()), true);EventCollector.Clear();", <%=MONITORING_DELAY%>);
        var scrollX = 0;
        var scrollY = 0;
        window.onscroll = function()
        {
            var x = (document.all) ? document.body.scrollLeft : window.pageXOffset;
            var y = (document.all) ? document.body.scrollTop : window.pageYOffset;
            if(x != scrollX) EventCollector.RegisterEvent(EventCollector.SCROLL_CHANGE_X, null, x);
            if(y != scrollY) EventCollector.RegisterEvent(EventCollector.SCROLL_CHANGE_Y, null, y);
        };
    </script>
<%
}

%>